Home | | Latest | About | Random
# Week 0 Notes.
Dear Class,
After Week 0 lecture and discussion, as well as the reading in sections 2 and 3 in the online free textbook [https://www.pic.ucla.edu/~mjandr/resources/10A-textbook/](https://www.pic.ucla.edu/~mjandr/resources/10A-textbook/) (remember to **RT(F)B**!), here are somethings you should have learned.
-B.
## Using an IDE to write and run your C++ code.
- If you are using MacOS, use XCode.
- If you are using Windows, use Visual Studio Community.
- In a pinch, you can also use [online GDB](https://www.onlinegdb.com/) for now.
- Or if you are not afraid of the terminal, you can use any text editor and compile in the terminal using a C++ compiler.
Regardless of whichever way you choose, you should figure this out ASAP. It is good to have the IDE and compiler to show you what your code is doing, so you can experiment and test your understanding of the C++ language. Please come to us ASAP to sort this out!
Of course, our goal in this course is to _become_ the compiler, so that we can look at a piece of C++ code and *understand and know what it is doing exactly*, even without our IDE and compiler!
## Shortest C++ program.
We start with the shortest C++ program that will compile and run. First, note that an empty file like the following
```cpp
```
**will not** build! A C++ will look for **one and exactly one** `main(){...}` function as its **entry point**, and execute the code inside the curly braces `{...}`. The code inside the curly braces `{...}` will consists of valid C++ instructions as sequence of C++ statements. Each statement ends with a semicolon `;`. In C++ additional spacings do not affect the code, so we can use them to our advantage and format the code. (Some programming languages are spacing sensitive.)
So here is the **"shortest C++ program"** that will compile and run:
```cpp
int main(){
return 0;
}
```
- This "shortest C++ program" can serve as a *"skeletal base"* to which we add more code to it.
- Here `main(){...}` is actually a C++ function. We will discuss functions more in detail later. A function as you recall from math is like a machine or factory that takes some input and return some output.
- Don't sweat this for now, but just to give you a taste. The mathematical function $f:\mathbb Z \to \mathbb Z , f(x) = x + 3$ is a function whose name is $f$ that takes in an integer $x \in \mathbb Z$, and spit out the integer $x + 3 \in \mathbb Z$. In C++ this will be written as
```cpp
int f(int x){
return x + 3;
}
```
- For now, you can think of things in the `()` will hold input parameters. Since we won't need our `main()` to take any inputs, we leave this empty. And it is required for the `main` function to return an integer (its *exit status code*) to the operating system (your computer) to indicated its status after its execution, with the value `0` to customarily mean "the program executed successfully and exited normally". This is why we have `return 0;` And as it this exit code is an integer, we declare the `main` function by writing `int main(){...}`.
- The statements inside the curly braces `{...}` will execute its statements sequentially in order, until it reaches `return` keyword (or some runtime error occurred).
- When defining a function, like `int main(){...}`, we do not need to end the final braces `}` with a semicolon. The ending matching `}` ends the definition.
- This "shortest C++ program" does **"nothing"**, except it will return an exit status code of `0` to the operating system.
- If you use an IDE like XCode it will show `Program ended with exit code: 0` after you press the "Play" button.
- If you use an IDE like Visual Studio it will show `...(process XXXX) exited with code 0 (0x0)....`
(Secret challenge. The "shortest C++ program" above is in fact not technically the shortest. The C++ standards allows it to be even shorter. Besides extraneous spacings that we can remove, we can actually remove more for it to still compile/build and run. Can you figure out what to remove? Experiment! Nevertheless, though it is C++ standard legal, I do not recommend you do this "even shorter" version!)
## Printing to the console.
To print to the console, we need to include this feature. We first write `#include ` on top of our do nothing code, and then write `std::cout << "Hello!";` in the `main` function (remember inside the curly braces `{..}` and before it hit `return`). Like this
```cpp
#include
int main(){
std::cout << "Hello!";
return 0;
}
```
If you hit play in your IDE (which builds an executable and runs said executable), you should see
```cmd
Hello!
```
as an output!
Note.
- Here things enclosed in a pair of matching double-quotes `"..."` is called a _string literal_. Try changing the string literal `"Hello!"` to another string literal of your choice, like `"I'm having a great day!"`.
- Note, every C++ statement ends in a semicolon `;` , so this applies to `std::cout << "Hello!";` as well
- The line `#include ` is actually something called a _preprocessing directive_ (not C++ statement), and you **do not** end this line with semicolon (despite some compiler might let you, some might not). Roughly speaking, before the code is compiled, the line `#include <...>` tells the compiler to to find this file `...` and effectively copy and paste that file at the location where this line `#include <...>` is read.
- The header file `` defines the interface (declarations) for printing to the console. When you build and run your program, the *linker* connects your compiled code to the standard library's binary, which contains the actual implementation of printing to the console. Later when we use `std::cout`, it is using something defined by this interface.
# `std::cout`.
## Printing on a new line `\n` and `std::endl`